import re

#https://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html
#正则设置组名

reNamedGroupTestStr = '标签：<a        href="/tag/中华武术/">中华武术</a>'

# 后面的填充 (?P=tagName) 必须在之前的表达式出现过该组名
reObj = re.compile(r'.+?<a\s*href="/tag/(?P<tagName>.+?)/">(?P=tagName)</a>') 
mObj = reObj.search(reNamedGroupTestStr)
print(mObj.span()) #获取匹配的字符串的起始和结束位置
print(mObj.groups()) #获取匹配的所有内容
if mObj:
    tagName = mObj.group('tagName') #根据组名获取匹配的值
    print(tagName)


routeUrl = "blog/user/1234/456" #之前在django遇到过的类似正则
routeReObj = re.compile(r'(?P<userId>\d+)')
#routeReObj = re.compile(r'\d+')
routeMObj = routeReObj.search(routeUrl)
routeMObj = routeReObj.findall(routeUrl) #匹配完所有
routeMObj = routeReObj.match(routeUrl)
print(routeMObj)
iterMObj = routeReObj.finditer(routeUrl) #搜索整个字符串并逐个返回匹配的项(_sre.SRE_Match 对象)
for i in iterMObj:
    print(i,i.groupdict()) #每个项都有组名


html = '<font color="#ff0000" value="20" size="14" >牛逼的python<span></font>'    
#html = "<font color='#ff0000' value='20' size='14' >牛逼的python</font>"  
reHtmlAttr = re.compile(r'[a-z]+\s*=\s*".*?"')
reHtmlTag = re.compile(r'<[a-zA-Z]+\s*.*?>|<[a-zA-Z]+\s*.*?>.*?</[a-zA-Z]+\s*>|</[a-zA-Z]+\s*>')    
reHtmlTag = re.compile(r'(?P<tagStart><[a-zA-Z]+\s*.*?>)(?P<tagContent>.*(?!<.*?>)\w+)(?P<tagEnd></[a-zA-Z]+\s*>)')    
#reHtmlTagEnd = re.compile(r'</[a-zA-Z]+\s*>')    

mTag = reHtmlTag.search(html)
print("dict",reHtmlTag.findall(html))
for i in reHtmlTag.finditer(html):
    print("html tag",i)

attrIter = reHtmlAttr.finditer(html)

for i in attrIter:
    print("html attr",i.group())

#不包含模式测试 ?!
s = 'Windows 3.1'
reWin = re.compile(r'Windows (?!95|98|NT|2000)') #匹配 Windows 后面不包含95|98|NT|2000 的windows字符串
m1 = reWin.search(s)
print('m1',m1)

s = "2019-03-01 23:59:59.000"
#([0-9]{4}-[0-9]{2}-[0-9]{2})([0-9]{2}:[0-9]{2}:[0-9]{2})
reObj=re.compile(r"([0-9]{4})-([0-9]{2})-([0-9]{2})\s([0-9]{2}):([0-9]{2}):([0-9]{2})")
mobj = reObj.search(s)
print("mobj",mobj)
if mobj:    
    print("group::",mobj.group())
    print("groups:",mobj.groups())